<template>
	<view class="container">
		<uni-forms-item v-if="showSelectDepart && type != 'gongwenliuzhuan'" label="承办部门" label-position="top">
		</uni-forms-item>
		<checkbox-group v-if="showSelectDepart && type != 'gongwenliuzhuan'" @change="departsChange">
			<view v-for="(yz,idx) in departNames" :key="idx">
				<view class="check-css"  >
					<checkbox :value="idx">{{yz}}</checkbox>
				</view>
				<view style="padding-bottom: 5px;"></view>
			</view>
		</checkbox-group>

		<uni-forms-item v-if="showSelectChepai" label="车牌号">
			<picker type="default" :range="chepaiList" @change="chepaiChange">
				<view class="picker-bg">{{chepaiNameStr()}}</view>
			</picker>
		</uni-forms-item>

		<uni-forms label-Position="top" v-if="showSelectMany">
			<uni-forms-item :label="title" required>
				<uni-row v-if="users.length !=0">
					<uni-col :span="8" v-for="(item, idx) in users" :key="idx">
						<view class="user-bg">
								<text>{{item.name}}</text>
						</view>
					</uni-col>
				</uni-row>
				<button @click="clickSelectMany" class="btn-add">{{selectBtnStr}}</button>
			</uni-forms-item>
		</uni-forms>

		<uni-forms label-Position="top" v-if="showSelectOne">
			<uni-forms-item :label="title" required>
				<view >
					<button  @click="clickSelectOne" class="btn-add" v-if="users.length != 0">{{users[0].name}}</button>
				</view>
				<view >
					<button @click="clickSelectOne" class="btn-add" v-if="users.length == 0">{{selectBtnStr}}</button>
				</view>
			</uni-forms-item>
		</uni-forms>

		<uni-forms label-Position="top" v-if="showMsg">
			<uni-forms-item label="审批意见">
				<uni-easyinput type="textarea" v-model="msg" placeholder="请输入审批意见" autoHeight/>
			</uni-forms-item>
		</uni-forms>
		
		<view style="padding: 20px;"></view>
		
		<uni-row v-if="typ == 0 && !showQueding">
			<uni-col :span="12"><button @click="rejectTask()" type="warn">拒绝</button></uni-col>
			<uni-col :span="12"><button @click="agreeTask()" type="primary">同意</button></uni-col>
		</uni-row>
		<uni-row v-if="typ == 0 && showQueding">
			<uni-col :span="24"><button @click="agreeTask()" type="primary">确定</button></uni-col>
		</uni-row>
		<uni-row v-if="typ == 1">
			<uni-col :span="24"><button @click="confirmTask()" type="primary">{{ chengbanBtnStr }}</button></uni-col>
		</uni-row>
	</view>
</template>

<script>

	import { websock } from '../../net/websock'

	export default {
		data() {
			return {
				taskid: 0,
				msg: '',
				title: '',
				selectBtnStr: '',
				chengbanBtnStr: '已完成',
				showMsg: true,
				typ: 0, // 0 审批 1承办
				userInfo: {},
				taskInfo: {},
				applyInfo: {},
				departList: [],
				chepaiList: [],
				chepaiIndex: 0,
				departNames: [],
				showSelectDepart: false,
				showSelectChepai: false,
				showSelectOne: false,
				showSelectMany: false,
				users: [],
				// 选中的部门
				selectedDeparts: [],
				// 部门筛选
				filterDeparts: [],
				showQueding: false
			}
		},
		async onLoad(params) {
			console.log('on load', params)
			this.taskid = params.taskid
			this.typ = 0
			if(params.type) {
				this.type = params.type;
			} else {
				this.type = "";
			}

			try {

				this.departList = await websock.getDepartList()
				for (let i = 1; i != this.departList.length; i++) {
					this.departNames.push(this.departList[i].name)
				}

				let chepaiList = await websock.chepaiList()
				for (let i = 0; i != chepaiList.length; i++) {
					this.chepaiList.push(chepaiList[i].name)
				}

				this.userInfo = await websock.getUserInfo()
				console.log('user info', this.userInfo)

				this.taskInfo = await websock.getTaskDetail({id: this.taskid})
				console.log('task info', this.taskInfo)

				this.typ = this.taskInfo.isChengban

				// 督办单 承办阶段 选择具体承办人
				// if (this.taskInfo.typ == 6 && this.taskInfo.isChengban) {
				// 	this.showSelectOne = true
				// 	this.title = '选择具体经办人'
				// 	this.selectBtnStr = '选择具体经办人'
				// 	this.chengbanBtnStr = '确定'
				// 	this.showMsg = false
				// }

				// 会议室 承办阶段 第一个承办人 选择具体经办人
				if (this.taskInfo.typ == 7 && this.taskInfo.isChengban && this.taskInfo.pos == 0) {
					this.showSelectOne = true
					this.title = '选择具体经办人'
					this.selectBtnStr = '选择具体经办人'
					this.chengbanBtnStr = '确定'
					this.showMsg = false
				}

				if (this.taskInfo.typ == 10 && this.userInfo.uid == 12) {
					this.showSelectChepai = true
				}

				// 传阅文件 承办阶段 周莉 选择多人 分发范围
				if (this.taskInfo.typ == 17) {
					if (this.taskInfo.isChengban) {
						if (this.userInfo.uid == 10) {
							this.showSelectMany = true
							this.title = '选择分发范围'
							this.selectBtnStr = '选择分发范围'
							this.chengbanBtnStr = '确定'
							this.showMsg = false
						} else {
							this.chengbanBtnStr = '确定'
						}
					}
				}
				// 公文流转
				if (this.taskInfo.typ == 18) {

					// 审核阶段 第一个审核人 标题和按钮显示拟办意见
					if (!this.taskInfo.isChengban && this.taskInfo.pos == 0) {
						this.title = '拟办意见'
						this.selectBtnStr = '拟办意见'
					}

					// 审核阶段 第三个审核人 显示选择分管领导
					if (!this.taskInfo.isChengban && this.taskInfo.pos == 2) {
						this.showSelectMany = true
						this.title = '选择分管领导'
						this.selectBtnStr = '选择分管领导'
						this.showMsg = false
						this.showQueding = true
						// this.filterDeparts = [1]
						this.filterDeparts = []
					}

					// 承办阶段 分管领导选择责任部门
					if (this.taskInfo.isChengban) {
						let applyInfo = await websock.gongwenLiuzhuanDetail({id: this.taskInfo.relevenceId})
						let chengbanren = applyInfo.chengbanren
						if (chengbanren['chengbanren' + this.taskInfo.pos] && chengbanren['chengbanren' + this.taskInfo.pos].fenguan) {
							this.showSelectDepart = true
							this.chengbanBtnStr = '确定'
						} else { // 分管领导选择责任部门 其他选择经办人
							this.showSelectMany = true
							this.title = '选择经办人'
							this.selectBtnStr = '选择经办人'
							this.chengbanBtnStr = '确定'
							this.filterDeparts = [chengbanren['chengbanren' + this.taskInfo.pos].departId || this.userInfo.department]
						}
					}

					// // 承办阶段 第一个承办人 选择承办部门负责人
					// if (this.taskInfo.isChengban && this.taskInfo.pos == 0) {
					// 	this.showSelectOne = true
					// 	this.title = '人员选择'
					// 	this.selectBtnStr = '选择承办部门负责人'
					// }
					// // 承办阶段 第二个承办人 选择经办人
					// if (this.taskInfo.isChengban && this.taskInfo.pos == 1) {
					// 	this.showSelectMany = true
					// 	this.title = '选择经办人'
					// 	this.selectBtnStr = '选择经办人'
					// }
				}
			} catch (err) {
				console.error(err)
			}
		},
		methods: {
			async agreeTask() {
				// 公文流转
				// 现在公文流转不选择承办部门了，只填写审批意见
				if(this.type != "gongwenliuzhuan") {
					if (this.taskInfo.typ == 18 && this.taskInfo.pos == 2 && this.users.length == 0) {
						return uni.showToast({title: '请选择分管领导'})
					}
				}
				try {
					await websock.agreeTask({ id: this.taskid, msg: this.msg, users: this.users, chepai: this.showSelectChepai ? this.chepaiList[this.chepaiIndex] : '' })
					uni.$emit('agree_task', this.taskid)
					uni.showToast({title: '操作成功'})
					uni.switchTab({url: '/pages/task/index'})
				} catch (err) {
					console.error(err)
					uni.showToast({title: '错误'})
				}
			},
			async rejectTask() {
				try {
					await websock.rejectTask({ id: this.taskid, msg: this.msg })
					uni.$emit('reject_task', this.taskid)
					uni.showToast({title: '操作成功'})
					uni.switchTab({url: '/pages/task/index'})
				} catch (err) {
					console.error(err)
					uni.showToast({title: '错误'})
				}
			},
			async confirmTask() {
				try {
					let departs = []
					if (this.selectedDeparts.length > 0) {
						for (let i = 0; i != this.selectedDeparts.length; i++) {
							departs.push({
								departId: this.departList[this.selectedDeparts[i] + 1].id,
								departName: this.departList[this.selectedDeparts[i] + 1].name,
								departLingdao: this.departList[this.selectedDeparts[i] + 1].zhijie_lingdao,
								departLingdaoName: this.departList[this.selectedDeparts[i] + 1].zhijie_lingdao_name
							})
						}
					}
					await websock.confirmTask({ id: this.taskid, msg: this.msg, users: this.users, departs })
					uni.$emit('confirm_task', this.taskid)
					uni.showToast({title: '操作成功'})
					uni.switchTab({url: '/pages/task/index'})
				} catch (err) {
					console.error(err)
					uni.showToast({title: '错误'})
				}
			},
			clickSelectMany() {
				uni.navigateTo({
					url: '/pages/common/contactSelect?selectedUsers=' + JSON.stringify(this.users || []) + '&departments=' + JSON.stringify(this.filterDeparts),
					events: {
						contactList: data => {
							console.log('select peitong', data)
							this.users = data.contactList
						}
					}
				})
			},
			clickSelectOne() {
				uni.navigateTo({
					url: '/pages/common/contactSelect?selectedUsers=' + JSON.stringify(this.users || []) + '&isSingle=1',
					events: {
						contactList: data => {
							console.log('select peitong', data)
							this.users = data.contactList
						}
					}
				})
			},
			departsChange(e) {
				console.log('departs change')
				this.selectedDeparts = e.detail.value
			},
			chepaiChange(e) {
				this.chepaiIndex = e.detail.value
			},
			chepaiNameStr() {
				return this.chepaiList[this.chepaiIndex] || ''
			}
		}
	}
</script>

<style>
	@import url("../common/common.css");
</style>
